/**
 * 自定义封装mqtt over webscoket插件
 * pub/sub 发布订阅模式
 */
MqttMq = {
	//通讯客户端
	client: null,
	//通讯域
	destination: null,
	//打开连接
	connect: function(ipAddress, user, password, destination, onConnect, onFailure, onMessageArrived, onConnectionLost) {
		//设置默认值
		var port = '61614';
		var clientId = user+(Math.floor(Math.random() * 100000));
		MqttMq.client = new Messaging.Client(ipAddress, Number(port), clientId);
		MqttMq.client.onConnect = onConnect;
		MqttMq.client.onMessageArrived = onMessageArrived;
		MqttMq.client.onConnectionLost = onConnectionLost;
		MqttMq.client.connect({
			userName: user,
			password: password,
			onSuccess: onConnect,
			onFailure: onFailure
		});
		MqttMq.destination = destination;
	},
	//关闭连接
	disconnect: function() {
		MqttMq.client.disconnect();
	},
	//发送消息
	sendmsg: function(text) {
		var message = new Messaging.Message(text);
		message.destinationName = MqttMq.destination;
		MqttMq.client.send(message);
	}
}